<template>
	<view class="mine">
		<titleName :name="name" :back-show="false" />
		<view class="top-img">
			<view class="my-msg">
				<image @click="updatePhone" :src="img" mode="">
				</image>
				<view class="msg">
					<view class="name" v-if="!admin">{{customername || '无'}}</view>
					<view class="phone" v-if="!admin">{{ hbmobile || '无'}}</view>
					<view class="name" v-if="admin">{{ departmentname || '无'}}</view>
					<view class="phone" v-if="admin">{{ mobile || '无'}}</view>
				</view>
			</view>
		</view>
		<view class="update-phone" style="margin-top: 30rpx;" @click="jumpUpdatePhone">
			<view class="left">
				<image class="icon" src="../../static/phone-icon.png" mode="">
				</image>
				<text>更换手机号</text>
			</view>
			<image class="arrow"
				src="https://splendid.oss-cn-beijing.aliyuncs.com/static/imghazardouswaste/right-arrow.png" mode="">
			</image>
		</view>
		<view class="update-phone" @click="jumpFeedBack">
			<view class="left">
				<image class="icon"
					src="https://splendid.oss-cn-beijing.aliyuncs.com/static/imghazardouswaste/zhili.png" mode="">
				</image>
				<text>{{admin?'个人信息':'意见反馈'}}</text>
			</view>
			<image class="arrow"
				src="https://splendid.oss-cn-beijing.aliyuncs.com/static/imghazardouswaste/right-arrow.png" mode="">
			</image>
		</view>
		<view class="update-phone" @click="jumpSupervision" v-if="!admin">
			<view class="left">
				<image class="icon"
					src="https://splendid.oss-cn-beijing.aliyuncs.com/static/imghazardouswaste/jianguanxinxi.png"
					mode="">
				</image>
				<text>监管信息</text>
			</view>
			<image class="arrow"
				src="https://splendid.oss-cn-beijing.aliyuncs.com/static/imghazardouswaste/right-arrow.png" mode="">
			</image>
		</view>
		<view class="btn-wrap">
			<button @click="loginout">{{tokenFloag?'退出登录':'去登录'}}</button>
		</view>
	</view>
</template>

<script setup>
	import titleName from '../../components/titleName.vue'
	import uploadFile from '../../request/uploadFile.js'
	import http from '../../request/index.js'
	import {
		onShow,
	} from '@dcloudio/uni-app'
	import {
		ref
	} from 'vue'
	let name = ref('我的')
	let customername = ref('')
	let img = ref("")
	let departmentname = ref('')
	let mobile = ref('')
	let admin = ref(false)
	let hbmobile = ref('')
	let tokenFloag = ref(false)
	let getAvator = () => {
		http({
			url: '/user/selfinfo',

		}).then((res) => {
			if (res.code == 0) {
				img.value = res.data.avatar ||
					'https://splendid.oss-cn-beijing.aliyuncs.com/static/imghazardouswaste/touxiang.png'
			}
		})
	}

	let updatePhone = () => {
		if (!tokenFloag.value) return

		uni.chooseImage({
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			count: 1,
			success: function(res) {
				let tempFilePaths = res.tempFilePaths

				for (var i = 0; i < tempFilePaths.length; i++) {
					uploadFile({
						filePath: tempFilePaths[i]
					}).then((result) => {

						if (result.code == 0) {
							http({
								url: '/user/changeavatar',
								data: {
									avatar: result.data.url
								}
							}).then((res) => {
								if (res.code == 0) {

									getAvator()
								}
							})
						}
					})
				}
			}
		});
	}
	onShow(() => {
		admin.value = uni.getStorageSync('admin')
		customername.value = uni.getStorageSync("customername1")
		// console.log(customername.value, 'fds');
		hbmobile.value = uni.getStorageSync("hbmobile")
		departmentname.value = uni.getStorageSync("departmentname")
		mobile.value = uni.getStorageSync("mobile")
		uni.getStorageSync("TOKEN") ? tokenFloag.value = true : tokenFloag.value = false
		if (tokenFloag.value) {
			getAvator()
		} else {
			img.value = "https://splendid.oss-cn-beijing.aliyuncs.com/static/imghazardouswaste/touxiang.png"
		}
	})

	let jumpUpdatePhone = () => {
		uni.navigateTo({
			url: "/minePageOther/updatephonenum/updatephonenum"
		})
	}
	let jumpFeedBack = () => {
		if (admin.value) {
			uni.navigateTo({
				url: "/minePageOther/supervisioninformation/supervisioninformation"
			})
		} else {
			uni.navigateTo({
				url: "/minePageOther/feedback/feedback"
			})
		}
	}
	let loginout = () => {
		if (admin.value) {
			let mobile = uni.getStorageSync('mobile') || ''
			uni.clearStorageSync()
			uni.setStorageSync('mobile', mobile)
		} else {
			uni.clearStorageSync()
		}
		uni.navigateTo({
			url: "/pages/login/login"
		})

	}
	let jumpSupervision = () => {
		uni.navigateTo({
			url: "/minePageOther/supervisioninformation/supervisioninformation"
		})
	}
</script>

<style lang="scss" scoped>
	.mine {
		width: 100vw;
		height: 100vh;

		.btn-wrap {
			margin-top: 333rpx;
			padding: 0 39rpx;

			button {
				width: 100%;
				height: 88rpx;
				background: #0874FA;
				border-radius: 44rpx;

				font-weight: 400;
				font-size: 34rpx;
				color: #FFFFFF;

			}
		}

		.update-phone {
			display: flex;
			align-items: center;
			padding-left: 40rpx;
			padding-right: 40rpx;
			justify-content: space-between;
			height: 134rpx;
			border-bottom: 2rpx solid #F3F5F7;

			.left {
				display: flex;
				align-items: center;

				.icon {
					width: 54rpx;
					height: 54rpx;
				}

				text {
					margin-left: 23rpx;
					font-weight: 500;
					font-size: 31rpx;
					color: #000000;

				}
			}

			.arrow {
				width: 15rpx;
				height: 25rpx;
				margin-left: 426rpx;
			}
		}

		.top-img {
			width: 100vw;
			height: 367rpx;
			background: url("../../static/mine-bgc.png") no-repeat;
			background-size: 100% 100%;
			padding-top: 186rpx;
			padding-left: 43rpx;

			.my-msg {
				display: flex;

				image {
					width: 95rpx;
					height: 95rpx;
				}

				.msg {
					display: flex;
					flex-direction: column;
					margin-left: 24rpx;
					justify-content: space-between;
					padding: 6rpx 0;

					.name {

						font-weight: 500;
						font-size: 36rpx;
						color: #FFFFFF;

					}

					.phone {

						font-weight: 600;
						font-size: 23rpx;
						color: #FFFFFF;

					}
				}
			}
		}
	}
</style>